﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace MetroCalendar.Controls
{
    public partial class TabControl : UserControl
    {
        private DispatcherTimer _timer;
        private List<TabTitleItem> _tabTitleItemList;
        private List<TabPage> _tabPageList;
        private TabPage _selectedTabPage;
        private TabPage _removedTabPage;
        private TabPage _addedTabPage;
        private Thickness _removedTabPageThickness;
        private Thickness _addedTabPageThickness;
        private int _shakeCount;
        private bool _isShaking;

        public TabControl()
        {
            InitializeComponent();

            _tabTitleItemList = new List<TabTitleItem>();
            _tabPageList = new List<TabPage>();

            _removedTabPageThickness = new Thickness();
            _addedTabPageThickness = new Thickness();

            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromMilliseconds(50);
            _timer.Tick += new EventHandler(OnTimerTick);
        }

        public void AddTabPage(TabPage tabPage)
        {
            _tabPageList.Add(tabPage);

            TabTitleItem item = new TabTitleItem();
            item.Title.Text = tabPage.Title;
            item.AssociatedPage = tabPage;
            item.Tap += new EventHandler<GestureEventArgs>(OnItemTap);
            TitlePanel.Children.Add(item);

            if (_selectedTabPage == null)
            {
                _selectedTabPage = tabPage;
                ContentPanel.Children.Add(tabPage);
                item.IsSelected = true;
            }
        }

        public void SetSelectedTabPage(TabPage tabPage)
        {
            _removedTabPage = _selectedTabPage;
            _addedTabPage = tabPage;

            _removedTabPageThickness = _removedTabPage.Margin;
            _addedTabPageThickness = _removedTabPageThickness;
            _addedTabPageThickness.Left += _removedTabPage.ActualWidth;
            _addedTabPage.Margin = _addedTabPageThickness;

            ContentPanel.Children.Add(_addedTabPage);

            _isShaking = false;
            _timer.Start(); // starts effects
            _selectedTabPage = tabPage;
        }

        void OnTimerTick(object sender, EventArgs e)
        {
            if (_isShaking)
            {
                _shakeCount--;
                if (_shakeCount >= 0)
                {
                    _addedTabPageThickness.Left = -_addedTabPageThickness.Left;
                    int decay = _addedTabPageThickness.Left > 0 ? - 1 : 1;
                    _addedTabPageThickness.Left += decay;
                }
                else
                {
                    _addedTabPageThickness.Left = 0;
                    _timer.Stop();

                    foreach (TabTitleItem item in TitlePanel.Children)
                    {
                        if (!item.IsEnabled && !item.IsSelected)
                        {
                            item.IsEnabled = true;
                        }
                    }
                }

                _addedTabPage.Margin = _addedTabPageThickness;
            }
            else
            {
                _removedTabPageThickness.Left -= 96;
                _removedTabPage.Margin = _removedTabPageThickness;

                _addedTabPageThickness.Left -= 96;
                _addedTabPage.Margin = _addedTabPageThickness;

                if (_addedTabPageThickness.Left <= 0)
                {
                    _isShaking = true;
                    _shakeCount = 5;
                    _addedTabPageThickness.Left = -_shakeCount;
                    ContentPanel.Children.Remove(_removedTabPage);
                }
            }
        }

        void OnItemTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (_timer.IsEnabled) return;

            TabTitleItem tappedItem = sender as TabTitleItem;
            if (tappedItem.AssociatedPage != _selectedTabPage)
            {
                foreach (TabTitleItem item in TitlePanel.Children)
                {
                    if (item == tappedItem)
                    {
                        item.IsSelected = true;
                    }
                    else
                    {
                        item.IsSelected = false;
                        item.IsEnabled = false;
                    }
                }

                SetSelectedTabPage(tappedItem.AssociatedPage);
            }
        }
    }
}
